//
//  YZMAnnDetailViewController.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/4/25.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMAnnDetailViewController.h"
#import "YZMResultView.h"
#import "YZClient+Account.h"
#import "YZGeneralResultModel.h"
#import "YZMAnnDetailModel.h"
#import <AFNetworking/AFNetworkReachabilityManager.h>
#import <DateTools/DateTools.h>

static NSString *const sYZMAnnDetailCellID = @"YZMAnnDetailCellID";

@interface YZMAnnDetailViewController ()

@property (nonatomic, strong) YZMResultView *resultView;
@property (nonatomic, strong) YZMAnnDetailModel *annDetail;
@property (nonatomic, assign) int noticeID;
@property (nonatomic, strong) UIWebView *webview;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIView *splitLine;

@end

@implementation YZMAnnDetailViewController

+ (instancetype)createWithParams: (NSDictionary *)params
{
    YZMAnnDetailViewController *ctrl = [[self alloc] init];
    ctrl.noticeID = [params[@"noticeId"] intValue];
    
    return ctrl;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    self.title = @"公告详情";
    [self createObjects];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
                                                 name:AFNetworkingReachabilityDidChangeNotification object:nil];
    [self fetchAnnouncementDetail];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)reachabilityChanged:(NSNotification *)note{
    if (!kNetworkNotReachability) {
        
        [self fetchAnnouncementDetail];
    }
}

- (void)createObjects{
    
    WS(ws)
    
    self.titleLabel = [YZMAppStyle labelWithLargeWhite];
    self.titleLabel.numberOfLines = 0;
    [self.view addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(ws.view).with.offset(ControlSpacing15);
        make.right.equalTo(ws.view).offset(-ControlSpacing15);
    }];
    
    self.timeLabel = [YZMAppStyle labelWithSmallLightGray];
    [self.view addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.titleLabel.mas_bottom).with.offset(ControlSpacing10);
        make.left.equalTo(ws.titleLabel);
    }];
    
    self.splitLine = [YZMAppStyle splitLine];
    self.splitLine.hidden = YES;
    [self.view addSubview:self.splitLine];
    [self.splitLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.timeLabel.mas_bottom).offset(10);
        make.left.right.equalTo(ws.titleLabel);
    }];
    
    self.webview = [UIWebView new];
    self.webview.backgroundColor = [UIColor clearColor];
    self.webview.opaque = NO;
//    self.webview.scalesPageToFit = YES;
    self.webview.scrollView.scrollEnabled = YES;
    self.webview.scrollView.bounces = NO;
    self.webview.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.webview];
    [self.webview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.splitLine.mas_bottom).offset(6);
        make.left.right.equalTo(ws.titleLabel);
        make.bottom.equalTo(ws.view);
    }];
    
    _resultView = [YZMResultView new];
    [self.view addSubview:_resultView];
    [_resultView mas_makeConstraints:^(MASConstraintMaker *maker){
        maker.center.width.equalTo(self.view);
    }];
    _resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
    _resultView.retryAction = ^(void){
        [ws fetchAnnouncementDetail];
    };
}

#pragma mark -- Network Request

- (void)fetchAnnouncementDetail {
    
    if (kNetworkNotReachability) {

        _resultView.status = YZM_RESULT_STATUS_FOR_NO_WIFI;
        self.annDetail = nil;
        return;
    }

    if (!self.annDetail) {
        
        _resultView.status = YZM_RESULT_STATUS_FOR_LOADING;
    }
    
    [YZClient fetchNoticeDetailWithNoticedID:self.noticeID Success:^(NSURLSessionDataTask *task, id responseObject) {
        
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        
        _resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
        
        YZGeneralResultModel *topModel = responseObject;
        
        if (topModel.success) {
            
            self.annDetail = topModel.data;
            
            if (self.annDetail) {

                self.titleLabel.text = self.annDetail.noticeTitle;
                self.timeLabel.text =  [self.annDetail.releaseTime formattedDateWithFormat:@"yyyy-MM-dd HH:mm:ss"];
                self.splitLine.hidden = NO;
#ifdef YZMicroStock
                
                self.annDetail.noticeContent = [NSString stringWithFormat:@"%@%@", @"<style>a,html{color:#FFF}</style>", self.annDetail.noticeContent];
#elif YZMicroStock_Agent_One
                
#endif
                
                [self.webview loadHTMLString:self.annDetail.noticeContent baseURL:nil];
            }
            
        } else {
            
            [_resultView syncStatusWithError:[NSError yz_errorWithMessage:topModel.resultMessage]];
        }
        
    } failure:^(NSError *error) {
            
        [_resultView syncStatusWithError:error];
    }];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
